﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using Moq;

namespace SkeletonNET.Common.Logging.Tests
{
    [TestClass]
    public class TestLogging
    {
        [TestInitialize]
        public void Initialize()
        {
            var container = new UnityContainer();
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
            container
                .RegisterInstance<IUnityContainer>(container);
        }

        [TestMethod]
        public void DoPublicLogMethodsWork()
        {
            var logHandlerMock = new Mock<LogHandler>();

            var exception = new InvalidOperationException();
            var message = "test";

            ServiceLocator.Current
                .GetInstance<IUnityContainer>()
                .RegisterInstance<LogHandler>(logHandlerMock.Object);

            logHandlerMock
                .Setup(x => x.Exception(exception, null))
                .Verifiable();
            logHandlerMock
                .Setup(x => x.Do(message, null))
                .Verifiable();

            Log<TestLogging>.Current.Exception(exception);
            Log<TestLogging>.Current.Do(message);

            logHandlerMock.Verify(x => x.Exception(exception, null));
            logHandlerMock.Verify(x => x.Do(message, null));
        }

        [TestMethod]
        public void TryToPassLogData()
        {
            var message = "test";
            var logHandlerMock = new Mock<LogHandler>();
            var logDataMock = new Mock<LogData>();
            logHandlerMock
                .Setup(x => x.Do(message, logDataMock.Object))
                .Verifiable();

            ServiceLocator.Current.GetInstance<IUnityContainer>().RegisterInstance<LogHandler>(logHandlerMock.Object);
            Log<TestLogging>.Current.Do(message, logDataMock.Object);

            Assert.IsTrue(logHandlerMock.Object.CallerType == typeof(TestLogging));
        }

        [TestMethod]
        public void TestConsoleLogHandler()
        {
            var message = "Test";
            var mock = new Mock<ToConsoleHandler>();
            mock.Setup(x => x.Do(message, null)).Verifiable();
            ServiceLocator.Current.GetInstance<IUnityContainer>().RegisterInstance<LogHandler>(mock.Object);
            Log<TestLogging>.Current.Do(message);
            mock.Verify(x => x.Do(message, null));
        }
    }
}
